package com.esp.soccertutor.uc;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;


import com.esp.soccertutor.R;
import com.esp.soccertutor.fbreader.fbreader.ChangeFontSizeAction;
import com.esp.soccertutor.fbreader.fbreader.FBReaderApp;
import com.esp.soccertutor.util.Log;
import com.esp.soccertutor.zlibrary.core.application.ZLApplication;
import com.esp.soccertutor.zlibrary.core.options.ZLIntegerRangeOption;

public class FontZoom_Dialog {

	private Context mContext = null;
	private LayoutInflater mLayoutInflater = null;
	private PopupWindow mPopupWindow = null;
	private boolean mIsShowing = false;
	
	private View mView;
	
	private int intfontsize;
	private SeekBar fontzoom_inout ;
	private TextView _tv_fontValue;
	private Button btnzoomok,btnzoomcancel;
	
	public boolean isShowing() { return mIsShowing; }
	
	public FontZoom_Dialog(Context context, LayoutInflater lo) {
		mContext = context;
		mLayoutInflater = lo;
		
		mView= mLayoutInflater.inflate(R.layout.font_zoom_dialog, null);
		
		 _tv_fontValue=(TextView) mView.findViewById(R.id._tv_fontValue);
	        
	        fontzoom_inout= (SeekBar) mView.findViewById(R.id.zoomcontrol);
	        fontzoom_inout.setMax(72);
	        initCheck();
	        fontzoom_inout.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
				public void onProgressChanged(SeekBar seekbar, int progress,boolean arg2) {
					
					 if(progress>=5){
					 	if(intfontsize<progress){
					 		intfontsize=progress;
							Log.print("AppHome : :onProgressChanged ::",""+intfontsize);
						_tv_fontValue.setText(String.valueOf(intfontsize)+"/72");
						
				 	}else{
				 		
				 		intfontsize=progress;
						Log.print("AppHome : :onProgressChanged ::",""+intfontsize);
						_tv_fontValue.setText(String.valueOf(intfontsize)+"/72");
					 	}
					}else{
					 _tv_fontValue.setText(String.valueOf(intfontsize+"/72"));
					}
				}

				public void onStartTrackingTouch(SeekBar arg0) {
				}

				public void onStopTrackingTouch(SeekBar arg0) {
					 _tv_fontValue.setText(String.valueOf(intfontsize+"/72"));
				}
			});
	        
	        
	        btnzoomok=(Button) mView.findViewById(R.id.btnzoomok);
	        btnzoomok.setOnClickListener(new OnClickListener() {
				
			
				public void onClick(View v) {
					
					System.out.println(""+intfontsize);
					FBReaderApp fbReader = (FBReaderApp)ZLApplication.Instance();
					new ChangeFontSizeAction(fbReader,intfontsize).run();
					hide();
				
				}
			});
	        btnzoomcancel=(Button) mView.findViewById(R.id.btnzoomcancel);
	        btnzoomcancel.setOnClickListener(new OnClickListener() {
				
	
				public void onClick(View v) {
					hide();
				}
			});
	        initCheck();
	}

	public void initCheck(){
    	
		ZLIntegerRangeOption FontSizeOption=new ZLIntegerRangeOption("Style", "Base:fontSize", 5, 72,5);
		Log.print("AppFontSize : : initCheck : :",""+FontSizeOption.getValue());
		fontzoom_inout.setProgress(FontSizeOption.getValue());
		intfontsize=FontSizeOption.getValue();
		_tv_fontValue.setText(String.valueOf(FontSizeOption.getValue())+"/72");
}
	
	public synchronized void show(View v) {
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		
		initCheck();
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        mPopupWindow.setWidth(display.getWidth());
        mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
}
